//
//  HNServiceDetailController.m
//  baidaifu
//
//  Created by 朱传正 on 2016/12/24.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNServiceDetailController.h"
#import "HNOrderDetailController.h"

@interface HNServiceDetailController ()



@end

@implementation HNServiceDetailController

- (IBAction)sureOrder:(UIButton *)sender {
    
    [self createOrder];
   
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"服务内容";
}

-(void)drawView{
    
    _sureBtn.layer.masksToBounds = YES;
    _sureBtn.layer.cornerRadius = 4;
    
    [self setOrderStatesView];
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 0.5)];
    lineLabel.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lineLabel];

}

-(void)initData{
    [self getServiceDetail];
}


-(void)setOrderStatesView{

    NSArray *imageStr = [NSArray arrayWithObjects:@"88570377512cff1.png",@"27574400482b33f.png",@"0956d5a491166cf.png",@"pay_zixun.png", nil];
     NSArray *nameStr = [NSArray arrayWithObjects:@"选择服务",@"下单",@"支付",@"咨询", nil];
    for (int i = 0; i<nameStr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreen_Width*(i+1)/4 - 17 - kScreen_Width/8, 10, 34, 34)];
        imageView.image = [UIImage imageNamed:imageStr[i]];
        [_orderStateView addSubview:imageView];
        
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreen_Width*i/4 , 55, kScreen_Width/4, 15)];
        nameLabel.numberOfLines = 1;
        nameLabel.clipsToBounds = YES;
        nameLabel.layer.cornerRadius = 4;
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:10];
        if (i == 0) {
            nameLabel.textColor = RGB(240, 160, 91);
        }else{
           nameLabel.textColor = RGBJ(181);
        }
        nameLabel.text = nameStr[i];
        [_orderStateView addSubview:nameLabel];
        
            //标题
            UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreen_Width*(i+1)/4 + imageView.frame.size.width/2 - kScreen_Width/8, 10 + imageView.frame.size.width/2, kScreen_Width*(i+1)/4 - imageView.frame.size.width, 2)];
            if (i == 0) {
                lineLabel.backgroundColor = RGB(242, 141, 0);
            }else if(i == nameStr.count - 1){
                lineLabel.backgroundColor = [UIColor whiteColor];
            }else {
                lineLabel.backgroundColor = RGBJ(177);
            }
            
            [_orderStateView addSubview:lineLabel];
    }
    
    
    
    
}




-(void)getServiceDetail{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:_goodsId forKey:@"goodsId"];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_get_consultDetail parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取咨询服务详情失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSDictionary * obj = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            
            _price.text = [NSString stringWithFormat:@"%@元/次",[obj objectForKey:@"shopPrice"]];
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取咨询服务详情失败"];
    }];
    
    
}


-(void)createOrder{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:_goodsId forKey:@"goodsId"];
    
    [param setObject:@"1" forKey:@"type"];
    
    [param setObject:@"1" forKey:@"goodsNumber"];
    
    
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_order_create parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"创建订单失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSDictionary * obj = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            
            HNOrderDetailController *orderDetailVC = [[HNOrderDetailController alloc]init];
            orderDetailVC.isService = YES;
            orderDetailVC.orderSn = [NSString stringWithFormat:@"%@",[obj objectForKey:@"orderSn"]];
            orderDetailVC.orderState = [NSString stringWithFormat:@"%@",[obj objectForKey:@"status"]];
            [self.navigationController pushViewController:orderDetailVC animated:YES];

            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"创建订单失败"];
    }];
    
    
}




@end
